#include<bits/stdc++.h>
using namespace std;

class Solution {
private:
    bool hs[10];
    bool JudgeRow (vector<vector<char>>& board, int row)
    {
        memset(hs, false, sizeof(hs));
        for (int i = 0; i < 9; i++) {
            if (board[row][i] == '.') {
                continue;
            }
            if (hs[board[row][i] - '0']) {
                return false;
            } else {
                hs[board[row][i] - '0'] = true;
            }
        }
        return true;
    }

    bool JudgeCol (vector<vector<char>>& board, int col)
    {
        memset(hs, false, sizeof(hs));
        for (int i = 0; i < 9; i++) {
            if (board[i][col] == '.') {
                continue;
            }
            if (hs[board[i][col] - '0']) {
                return false;
            } else {
                hs[board[i][col] - '0'] = true;
            }
        }
        return true;
    }

    bool JudgeBlock (vector<vector<char>>& board, int row, int col)
    {
        memset(hs, false, sizeof(hs));
        for (int i = row; i < row + 3; i++) {
            for (int j = col; j < col + 3; j++) {
                if (board[i][j] == '.') {
                    continue;
                }
                if (hs[board[i][j] - '0']) {
                    return false;
                } else {
                    hs[board[i][j] - '0'] = true;
                }
            }
        }
        return true;
    }

public:
    bool isValidSudoku(vector<vector<char>>& board) {
        for (int i = 0; i < 9; i++) {
            if (!JudgeRow(board, i) || !JudgeCol(board, i)) {
                return false;
            }
        }
        for (int i = 0; i < 9; i += 3) {
            for (int j = 0; j < 9; j += 3) {
                if (!JudgeBlock(board, i, j)) {
                    return false;
                }
            }
        }
        return true;
    }
};

/**
 * @brief 2022.7.20
 * 模拟
 */